package Agent;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import eis.iilang.Action;
import eis.iilang.Identifier;

public class Util {

	public static String XMLToString(Node node)
	{
		try {
            Source source = new DOMSource(node);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } 
        catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } 
        catch (TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
	
	public static  Document actionToXML(Action action , int idAction) {

		String actionType = null;
		actionType = action.getName();
		
		String actionParameter = null;
		if ( action.getParameters().size() != 0)
			actionParameter = action.getParameters().element().toProlog();
		
		String actionId = null;
		actionId = "" + idAction;
		
		Document doc = null;
		try {
			
			DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
			doc = documentbuilderfactory.newDocumentBuilder().newDocument();
			Element root = doc.createElement("message");
			root.setAttribute("type","action");
			doc.appendChild(root);
			
			Element auth = doc.createElement("action");
			auth.setAttribute("type",actionType);
			if ( actionParameter != null )
				auth.setAttribute("param",actionParameter);
			auth.setAttribute("id",actionId);
			root.appendChild(auth);
			
		} catch (ParserConfigurationException e) {
			System.err.println("Erreur Parsing Action : "+e.getStackTrace());
			return null;
		}
		
		return doc;
	
	}

	static public Action gotoAction(String nodeName) {
		return new Action("goto",new Identifier(nodeName));
	}

	/**
	 * Yields a valid skip-action.
	 * @return
	 */
	static public Action skipAction() {
		return new Action("skip");
	}

	/**
	 * Yields a valid probe-action.
	 * @return
	 */
	static public Action probeAction() {
		return new Action("probe");
	}
	
	/**
	 * Yields a valid probe-action for a specific node.
	 * @return
	 */
	static public Action probeAction(String nodeName) {
		return new Action("probe", new Identifier(nodeName));
	}

	/**
	 * Yields a valid survey-action.
	 * @return
	 */
	static public Action surveyAction() {
		return new Action("survey");
	}

	/**
	 * Yields a valid inspect-action.
	 * @return
	 */
	static public Action inspectAction() {
		return new Action("inspect");
	}
	
	/**
	 * Yields a valid inspect-action for a specific agent.
	 * @return
	 */
	static public Action inspectAction(String agentName) {
		return new Action("inspect", new Identifier(agentName));
	}

	/**
	 * Yields a valid parry-action.
	 * @return
	 */
	public static Action parryAction() {
		return new Action("parry");
	}
	
	/**
	 * Yields a valid attack action.
	 * @param entityName
	 * @return
	 */
	static public Action attackAction(String entityName) {
		return new Action("attack",new Identifier(entityName));
	}

	/**
	 * Yields a valid buy action.
	 * @param item 
	 * @return
	 */
	static public Action buyAction(String item) {
		return new Action("buy",new Identifier(item));
	}

	/**
	 * Yields a valid repair action.
	 * @param entity
	 * @return
	 */
	static public Action repairAction(String entity) {
		return new Action("repair",new Identifier(entity));
	}
	
	/**
	 * Yields a valid recharge action.
	 * @param entity
	 * @return
	 */
	static public Action rechargeAction() {
		return new Action("recharge");
	}
	
}
